﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JustAgile.Html.Linq;
using Unicoen.Languages.JavaScript.ModelFactories;
using Unicoen.Model;

namespace HtmlInputExtractor.Common {
	public static class JavaScriptAnalyzer {
		public static IEnumerable<IUnifiedElement> ParseJavaScript(HDocument hdoc) {
			return hdoc.Descendants("script")
					.Where(e => {
						var attr = e.Attribute("type");
						return attr != null && attr.Value == "text/javascript";
					})
					.Select(e => {
						var srcAttr = e.Attribute("src");
						if (srcAttr == null) {
							var hcom = e.Nodes().OfType<HComment>().First();
							return JavaScriptModelFactory.Instance.Generate(hcom.Value);
						}
						return null;
					})
					.ToList();
		}
	}
}
